package com.hodgepodge.admin.api;


import com.hodgepodge.api.service.TagService;
import com.hodgepodge.commons.base.entity.Result;
import com.hodgepodge.commons.base.entity.ValidList;
import com.hodgepodge.commons.entity.Tag;
import com.hodgepodge.commons.validator.group.Insert;
import com.hodgepodge.commons.validator.group.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 标签表 Admin接口
 * </p>
 *
 * @author jiangjian
 * @since 2021-02-04
 */
@Api(tags = "标签接口-admin")
@RestController
@RequestMapping(value = "admin/tag")
public class TagApi {

    @Resource
    private TagService tagService;

    /**
     * 根据标签ID获取标签信息
     *
     * @param tagId 标签ID
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "根据标签ID获取标签信息", notes = "根据标签ID获取标签信息")
    @GetMapping("getTagById")
    public Result<Tag> getTagById(@ApiParam(name = "tagId", value = "标签ID", required = true) @RequestParam String tagId) {
        Tag tag = tagService.getById(tagId);
        return Result.success(tag);
    }

    /**
     * 获取所有标签列表信息
     *
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "获取所有标签列表信息", notes = "获取所有标签列表信息")
    @GetMapping("getList")
    public Result<List<Tag>> getList() {
        List<Tag> tags = tagService.list();
        return Result.success(tags);
    }

    /**
     * 根据条件获取标签列表信息
     *
     * @param tag 条件参数
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "根据条件获取标签列表信息", notes = "根据条件获取标签列表信息")
    @PostMapping("getListByCondition")
    public Result<List<Tag>> getListByCondition(@RequestBody Tag tag) {
        List<Tag> tags = tagService.getListByCondition(tag);
        return Result.success(tags);
    }

    /**
     * 新增或更新标签
     *
     * @param tag 条件参数
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "新增或更新标签", notes = "新增或更新标签")
    @PostMapping("save")
    public Result<Tag> insertOrUpdate(@RequestBody Tag tag) {
        boolean flag = tagService.saveOrUpdate(tag);
        if (flag) {
            return Result.success("保存成功");
        } else {
            return Result.fail("保存失败");
        }
    }

    /**
     * 新增单个标签
     *
     * @param tag 新增参数
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "新增单个标签", notes = "新增单个标签")
    @PostMapping("insert")
    public Result<Tag> insert(@RequestBody @Validated(Insert.class) Tag tag) {
        boolean flag = tagService.save(tag);
        if (flag) {
            return Result.success("新增成功");
        } else {
            return Result.fail("新增失败");
        }
    }

    /**
     * 批量新增标签
     *
     * @param tags 新增参数列表
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "批量新增标签", notes = "批量新增标签")
    @PostMapping("insertBatch")
    public Result<Tag> insertBatch(@RequestBody @Validated(Insert.class) ValidList<Tag> tags) {
        boolean flag = tagService.saveBatch(tags);
        if (flag) {
            return Result.success("新增成功");
        } else {
            return Result.fail("新增失败");
        }
    }

    /**
     * 根据标签ID更新标签信息
     *
     * @param tag 条件参数
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "根据标签ID更新标签信息", notes = "根据标签ID更新标签信息")
    @PutMapping("updateById")
    public Result<Tag> updateById(@RequestBody @Validated(Update.class) Tag tag) {
        boolean flag = tagService.updateById(tag);
        if (flag) {
            return Result.success("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    /**
     * 根据标签ID删除标签信息
     *
     * @param tagId 标签ID
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "根据标签ID删除标签信息", notes = "根据标签ID删除标签信息")
    @DeleteMapping("deleteById")
    public Result<Tag> deleteById(@ApiParam(name = "tagId", value = "标签ID", required = true) @RequestParam String tagId) {
        boolean flag = tagService.removeById(tagId);
        if (flag) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    /**
     * 根据标签ID列表批量删除标签信息
     *
     * @param tagIds 标签ID列表,用逗号分隔
     * @return 响应结果
     * @author jiangjian
     * @date 2021-02-04
     */
    @ApiOperation(value = "根据标签ID列表批量删除标签信息", notes = "根据标签ID列表批量删除标签信息")
    @DeleteMapping("deleteByIds")
    public Result<Tag> deleteByIds(@ApiParam(name = "tagIds", value = "标签ID列表,用逗号分隔", required = true) @RequestParam List<String> tagIds) {
        boolean flag = tagService.removeByIds(tagIds);
        if (flag) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}
